package com.yike.user.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.common.pagination.PageInfo;
import com.yike.common.pagination.Paging;
import com.yike.user.dto.*;
import com.yike.user.mapper.PlatformDataMapper;
import com.yike.user.param.*;
import com.yike.user.service.PlatformReportService;
import com.yike.user.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 平台数据管理
 *
 * @author Sakura
 * @since 2024-11-18
 */
@Slf4j
@Service
public class PlatformReportServiceImpl implements PlatformReportService {

    @Autowired
    private PlatformDataMapper platformDataMapper;

    @Override
    public Paging<SalesReportDataVo> getSalesReportList(PlatformReportDataParam platformReportDataParam) throws Exception {
        Page<SalesReportDataVo> page = new PageInfo<>(platformReportDataParam);
        IPage<SalesReportDataVo> iPage = platformDataMapper.getSalesReportList(page, platformReportDataParam);
        return new Paging<SalesReportDataVo>(iPage);
    }

    @Override
    public SalesReportDataVo getSalesReportCount(PlatformReportDataParam platformReportDataParam) throws Exception {
        return platformDataMapper.getSalesReportDataVo(platformReportDataParam);
    }

    @Override
    public void exportSalesReport(HttpServletResponse response, PlatformReportDataParam platformReportDataParam) throws Exception {
        // 先获取数据
        List<SalesReportDataDTO> exportSalesReports = platformDataMapper.getExportSalesReports(platformReportDataParam);

        // 设置导出参数
        ExportParams exportParams = new ExportParams(null, "销售报表");

        // 生成工作簿
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SalesReportDataDTO.class, exportSalesReports);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("销售报表.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 写入到响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @Override
    public Paging<AgentReportDataVo> getAgentReportList(PlatformReportDataParam platformReportDataParam) throws Exception {
        Page<AgentReportDataVo> page = new PageInfo<>(platformReportDataParam);
        IPage<AgentReportDataVo> iPage = platformDataMapper.getAgentReportList(page, platformReportDataParam);
        return new Paging<AgentReportDataVo>(iPage);
    }

    @Override
    public AgentReportDataVo getAgentReportCount(PlatformReportDataParam platformReportDataParam) throws Exception {
        return platformDataMapper.getAgentReportDataVo(platformReportDataParam);
    }

    @Override
    public void exportAgentReport(HttpServletResponse response, PlatformReportDataParam platformReportDataParam) throws Exception {
        // 先获取数据
        List<AgentReportDataDTO> exportAgentReports = platformDataMapper.getExportAgentReports(platformReportDataParam);

        // 设置导出参数
        ExportParams exportParams = new ExportParams(null, "代理商报表");

        // 生成工作簿
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, AgentReportDataDTO.class, exportAgentReports);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("代理商报表.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 写入到响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @Override
    public Paging<FinanceReportDataVo> getFinanceReportList(PlatformReportDataParam platformReportDataParam) throws Exception {
        Page<FinanceReportDataVo> page = new PageInfo<>(platformReportDataParam);
        IPage<FinanceReportDataVo> iPage = platformDataMapper.getFinanceReportList(page, platformReportDataParam);
        return new Paging<FinanceReportDataVo>(iPage);
    }

    @Override
    public FinanceReportDataVo getFinanceReportCount(PlatformReportDataParam platformReportDataParam) throws Exception {
        return platformDataMapper.getFinanceReportCount(platformReportDataParam);
    }

    @Override
    public void exportFinanceReport(HttpServletResponse response, PlatformReportDataParam platformReportDataParam) throws Exception {
        // 先获取数据
        List<FinanceReportDataDTO> exportFinanceReports = platformDataMapper.getExportFinanceReports(platformReportDataParam);

        // 设置导出参数
        ExportParams exportParams = new ExportParams(null, "财务报表");

        // 生成工作簿
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, FinanceReportDataDTO.class, exportFinanceReports);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("财务报表.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 写入到响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @Override
    public Paging<SalesRankDataVo> getSalesCategoryRankList(PlatformReportDataParam platformReportDataParam) throws Exception {
        Page<SalesRankDataVo> page = new PageInfo<>(platformReportDataParam);
        IPage<SalesRankDataVo> iPage = platformDataMapper.getSalesCategoryRankList(page, platformReportDataParam);
        return new Paging<SalesRankDataVo>(iPage);
    }

    @Override
    public void exportSalesCategoryRank(HttpServletResponse response, PlatformReportDataParam platformReportDataParam) throws Exception {
        // 先获取数据
        List<SalesCategoryRankDataDTO> exportSalesCategoryRanks = platformDataMapper.getExportSalesCategoryRanks(platformReportDataParam);

        // 设置导出参数
        ExportParams exportParams = new ExportParams(null, "销售排行（分类排行）");

        // 生成工作簿
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SalesCategoryRankDataDTO.class, exportSalesCategoryRanks);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("销售排行（分类排行）.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 写入到响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @Override
    public Paging<SalesRankDataVo> getSalesGoodsRankList(PlatformReportDataParam platformReportDataParam) throws Exception {
        Page<SalesRankDataVo> page = new PageInfo<>(platformReportDataParam);
        IPage<SalesRankDataVo> iPage = platformDataMapper.getSalesGoodsRankList(page, platformReportDataParam);
        return new Paging<SalesRankDataVo>(iPage);
    }

    @Override
    public void exportSalesGoodsRank(HttpServletResponse response, PlatformReportDataParam platformReportDataParam) throws Exception {
        // 先获取数据
        List<SalesGoodsRankDataDTO> exportSalesGoodsRanks = platformDataMapper.getExportSalesGoodsRanks(platformReportDataParam);

        // 设置导出参数
        ExportParams exportParams = new ExportParams(null, "销售排行（商品排行）");

        // 生成工作簿
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SalesGoodsRankDataDTO.class, exportSalesGoodsRanks);

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("销售排行（商品排行）.xlsx", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);

        // 写入到响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

}
